import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_high_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_select_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/data/entities/home/duty/duty_schedule_config_entity.dart';
import 'package:safetymonitor/app/data/entities/home/duty/system_user_page_entity.dart';
import 'package:safetymonitor/router/app_pages.dart';

import 'controller.dart';

///开始交接班
class StartTheShiftPage extends StatelessWidget {
  StartTheShiftPage({super.key});

  final controller = Get.put(StartTheShiftController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<StartTheShiftController>(
        init: controller,
        builder: (controller) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '交接班',
            ),
            body: Container(
              color: const Color(0xFFF7FBFE),
              padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 14.w),
              child: ListView(
                children: [
                  CustomTitleChartPage(
                    title: '交班人员',
                    child: _tobWidget(),
                  ),
                  CustomTitleChartPage(
                    title: '接班人员',
                    child: _centerWidget(),
                  ),
                  CustomTitleChartPage(
                    title: '重大事件填报',
                    child: _bottomWidget(),
                  ),
                  AppGaps.vGap30,
                ],
              ),
            ),
            bottomSheet: Container(
              width: double.infinity,
              color: Colors.white,
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColors.blue_button,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(22.r),
                  ),
                ),
                onPressed: controller.confirm,
                child: Text(
                  '确定',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
          );
        });
  }

  Widget _tobWidget() {
    return Column(
      children: [
        CustomSelectPage(
          isEnabled: false,
          isRequired: false,
          selectText:
              '${controller.changeShiftInfoCreateEntity.handoverPerson}',
          title: '交班人员',
          hintText: '请选择',
          onCallBack: () {},
        ),
        CustomSelectPage(
          isEnabled: false,
          isRequired: false,
          selectText:
              '${controller.changeShiftInfoCreateEntity.handoverPersonPhone}',
          title: '交班人员电话',
          hintText: '请选择',
          onCallBack: () {},
        ),
        CustomSelectPage(
          isEnabled: true,
          isRequired: false,
          selectText: controller.changeShiftInfoCreateEntity.onDutyTime == ''
              ? ''
              : '${controller.changeShiftInfoCreateEntity.onDutyTime}',
          title: '上岗时间',
          hintText: '请选择',
          onCallBack: () {
            controller.selectOnDutyTime();
          },
        ),
        CustomSelectPage(
          isEnabled: true,
          selectText: controller.changeShiftInfoCreateEntity.handoverTime == ''
              ? ''
              : '${controller.changeShiftInfoCreateEntity.handoverTime}',
          title: '交班时间',
          hintText: '请选择',
          onCallBack: () {
            controller.selectHandoverTime();
          },
        ),
        CustomSelectPage(
          isEnabled: true,
          isRequired: false,
          selectText:
              '${controller.changeShiftInfoCreateEntity.handoverScheduleName}',
          title: '班次名称',
          hintText: '请选择',
          onCallBack: () {
            Get.toNamed(Routes.scheduleConfigSelectionPage, arguments: {
              'isRadio': true,
            })?.then((value) {
              final List<DutyScheduleConfigEntity> selectedList =
                  value as List<DutyScheduleConfigEntity>;
              controller.changeShiftInfoCreateEntity.handoverScheduleName =
                  selectedList[0].scheduleName!;
              controller.changeShiftInfoCreateEntity.handoverScheduleConfigId =
                  selectedList[0].id!;
              controller.update();
            });
          },
        ),
        CustomSelectPage(
          isEnabled: false,
          isRequired: false,
          selectText:
              '${controller.changeShiftInfoCreateEntity.handoverPersonDept}',
          title: '所属部门',
          hintText: '请选择',
          onCallBack: () {},
        ),
        CustomSelectPage(
          isEnabled: true,
          isRequired: false,
          selectText:
              '${controller.changeShiftInfoCreateEntity.handoverLeader}',
          title: '值班领导',
          hintText: '请选择',
          onCallBack: () {
            Get.toNamed(Routes.systemUserPeopleSelectionSelectionPage,
                arguments: {
                  'isRadio': true,
                })?.then((value) {
              final List<SystemUserPageEntity> selectedList =
                  value as List<SystemUserPageEntity>;
              controller.  changeShiftInfoCreateEntity.handoverLeaderId  = selectedList[0].id!; //获取领导id
              controller.changeShiftInfoCreateEntity.handoverLeader = selectedList[0].nickname!; //获取领导名字
              controller.changeShiftInfoCreateEntity.handoverLeaderPhone = selectedList[0].mobile!; //获取领导手机号
                controller. majorEventInfoCreateEntity.dutyLeader = selectedList[0].nickname!; //获取领导名字
              controller. majorEventInfoCreateEntity.dutyLeaderPhone = selectedList[0].mobile!; //获取领导手机号
              controller.update();
            });
          },
        ),
        CustomInputPage(
          title: '值班领导电话：',
          isRequired: false,
          textInputType: TextInputType.number,
          isEnabled: true,
          text: '${controller.changeShiftInfoCreateEntity.handoverLeaderPhone}',
          hintText: '请输入',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.handoverLeaderPhone = value;
          },
        ),
      ],
    );
  }

  Widget _centerWidget() {
    return Column(
      children: [
        CustomSelectPage(
          isEnabled: true,
          selectText:
              '${controller.changeShiftInfoCreateEntity.takeoverPerson}',
          title: '接班人员',
          hintText: '请选择',
          onCallBack: () {
            Get.toNamed(Routes.systemUserPeopleSelectionSelectionPage,
                arguments: {
                  'isRadio': true,
                })?.then((value) {
              final List<SystemUserPageEntity> selectedList =
                  value as List<SystemUserPageEntity>;
              controller.changeShiftInfoCreateEntity.takeoverPersonId = selectedList[0].id; //获取接班人员名字
              controller.changeShiftInfoCreateEntity.takeoverPerson = selectedList[0].nickname!; //获取接班人员名字
              controller.changeShiftInfoCreateEntity.takeoverPersonPhone = selectedList[0].mobile!; //获取接班人员手机号
              controller.changeShiftInfoCreateEntity.takeoverPersonDept = selectedList[0].deptName!; //获取接班人员所属部门
              controller.update();
            });
          },
        ),
        CustomInputPage(
          title: '接班人员电话：',
          textInputType: TextInputType.number,
          isEnabled: true,
          text: '${controller.changeShiftInfoCreateEntity.takeoverPersonPhone}',
          hintText: '请输入',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.takeoverPersonPhone = value;
          },
        ),
        CustomSelectPage(
          isEnabled: true,
          selectText: controller.changeShiftInfoCreateEntity.takeoverTime == ''
              ? ''
              : '${controller.changeShiftInfoCreateEntity.takeoverTime}',
          title: '接班时间',
          hintText: '请选择',
          onCallBack: () {
            controller.selectJBTime();
          },
        ),
        CustomSelectPage(
          isEnabled: true,
          selectText:
              '${controller.changeShiftInfoCreateEntity.takeoverScheduleName}',
          title: '班次名称',
          hintText: '请选择',
          onCallBack: () {
            Get.toNamed(Routes.scheduleConfigSelectionPage, arguments: {
              'isRadio': true,
            })?.then((value) {
              final List<DutyScheduleConfigEntity> selectedList =
                  value as List<DutyScheduleConfigEntity>;
              controller.changeShiftInfoCreateEntity.takeoverScheduleName = selectedList[0].scheduleName!;
              controller.changeShiftInfoCreateEntity.takeoverScheduleConfigId = selectedList[0].id!;
              controller.update();
            });
          },
        ),
        CustomInputPage(
          isRequired: false,
          title: '所属部门：',
          // textInputType: TextInputType.number,
          isEnabled: true,
          text: '${controller.changeShiftInfoCreateEntity.takeoverPersonDept}',
          hintText: '请输入',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.takeoverPersonDept = value;
          },
        ),
        CustomSelectPage(
          isEnabled: true,
          selectText:
              '${controller.changeShiftInfoCreateEntity.takeoverLeader}',
          title: '值班领导',
          hintText: '请选择',
          onCallBack: () {
            Get.toNamed(Routes.systemUserPeopleSelectionSelectionPage,
                arguments: {
                  'isRadio': true,
                })?.then((value) {
              final List<SystemUserPageEntity> selectedList =
                  value as List<SystemUserPageEntity>;
              controller.changeShiftInfoCreateEntity.takeoverLeaderId = selectedList[0].id; //获取领导名字
              controller.changeShiftInfoCreateEntity.takeoverLeader = selectedList[0].nickname!; //获取领导名字
              controller.changeShiftInfoCreateEntity.takeoverLeaderPhone = selectedList[0].mobile!; //获取领导手机号
              controller.update();
            });
          },
        ),
        CustomInputPage(
          title: '值班领导电话：',
          isRequired: false,
          textInputType: TextInputType.number,
          isEnabled: true,
          text: '${controller.changeShiftInfoCreateEntity.takeoverLeaderPhone}',
          hintText: '请输入',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.takeoverLeaderPhone = value;
          },
        ),
      ],
    );
  }

  Widget _bottomWidget() {
    return Column(
      children: [
        CustomSelectPage(
          isEnabled: true,
          isRequired: false,
          selectText: '${controller.changeShiftInfoCreateEntity.eventTime}',
          title: '事件发生时间',
          hintText: '请选择',
          onCallBack: () {
            controller.showDatePickerEventTime();
          },
        ),
        CustomInputPage(
          title: '事件发生地点：',
          isRequired: false,
          isEnabled: true,
          text: '${controller.changeShiftInfoCreateEntity.eventAddr}',
          hintText: '请输入',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.eventAddr = value;
            controller.majorEventInfoCreateEntity.eventAddr = value;
          },
        ),
        CustomHighInputPage(
          isEnabled: true,
          title: '事件描述：',
          isRequired: false,
          text: '${controller.changeShiftInfoCreateEntity.eventDesc}',
          hintText: '请输入事件描述',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.eventDesc = value;
            controller.majorEventInfoCreateEntity.eventDesc = value;
          },
        ),
        CustomHighInputPage(
          isEnabled: true,
          title: '事件原因：',
          isRequired: false,
          text: '${controller.changeShiftInfoCreateEntity.eventReason}',
          hintText: '请输入事件原因',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.eventReason = value;
            controller.majorEventInfoCreateEntity.eventReason = value;
          },
        ),
        CustomHighInputPage(
          isEnabled: true,
          title: '处置措施：',
          isRequired: false,
          text: '${controller.changeShiftInfoCreateEntity.measure}',
          hintText: '请输入处置措施',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.measure = value;
            controller.majorEventInfoCreateEntity.measure = value;
          },
        ),
        CustomHighInputPage(
          isEnabled: true,
          title: '备注：',
          isRequired: false,
          isShowDivider: false,
          text: '${controller.changeShiftInfoCreateEntity.remark}',
          hintText: '请输入备注',
          onCallBack: (value) {
            controller.changeShiftInfoCreateEntity.remark = value;
            controller.majorEventInfoCreateEntity.remark = value;
          },
        ),
      ],
    );
  }
}
